package com.ymatou.iapi.topicrecbizer.testcase;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.topicrecbizer.parameter.HotListBean;
import com.ymatou.iapi.topicrecbizer.service.HotListCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;

public class Ts_HotList {
	private static HotListBean hotlistBean;
	private static HotListCall hotlistCall;
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("24H买手榜接口");
	}

	@Before
	public void caseUp() {
		hotlistBean = new HotListBean();
		hotlistCall = new HotListCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(hotlistCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_HotList_001() {
		Logger.start(true,"24H买手榜-最有逼格-检查接口返回公共信息 ");
		try {
			
			//设置参数
			String requestid="111111";
			String type = "bigger";
			Date date=new Date();
			DateFormat format= new SimpleDateFormat("yyyyMMddHHmmss" );
			String timenow=format.format( date);

			
			hotlistBean.setInfo("userid:11,cookieid:22,idfa:33,imei:44,yid:3333,timenow:"+timenow+",type:"+type+"");
			hotlistBean.setAppkey("1");
			hotlistBean.setRequestid(requestid);
			hotlistBean.setModule("seller_hot_list");
			hotlistBean.setPlatform("appbuyer");
			hotlistBean.setLimit("0,10");
			
			hotlistCall.setData(hotlistBean);
			hotlistCall.callService();
			
			Logger.verifyEquals("OK",hotlistCall.getCommonInfo("status") , "检查接口状态值");
			Logger.verifyNotNull(hotlistCall.getCommonInfo("took"), "took不为空");
			Logger.verifyEquals(false, hotlistCall.getCommonInfoBoo("downgrade"), "验证downgrade返回false");
			Logger.verifyEquals(false, hotlistCall.getCommonInfoBoo("timeout"), "验证timeout返回flase");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_HotList_002() {
		Logger.start(true,"24H买手榜-最有逼格-检查接口返回上一次更新时间是否24小时之内 ");
		try {
			//设置参数
			String requestid="111111";
			String type = "bigger";
			Date date=new Date();
			DateFormat format= new SimpleDateFormat("yyyyMMddHHmmss" );
			String timenow=format.format( date);

			
			hotlistBean.setInfo("userid:11,cookieid:22,idfa:33,imei:44,yid:3333,timenow:"+timenow+",type:"+type+"");
			hotlistBean.setAppkey("1");
			hotlistBean.setRequestid(requestid);
			hotlistBean.setModule("seller_hot_list");
			hotlistBean.setPlatform("appbuyer");
			hotlistBean.setLimit("0,10");
			
			hotlistCall.setData(hotlistBean);
			hotlistCall.callService();
			
			if (hotlistCall.getStatus() != "OK") {
				Logger.verifyEquals("OK", hotlistCall.getStatus(), "接口返回状态是否异常");
			} else {

				JSONArray hits = hotlistCall.getHits();
				JSONObject searchInfo = (JSONObject) hotlistCall.getSearchInfo();

				String nextUpdateTime = searchInfo.getString("nextUpdateTime");

				SimpleDateFormat df = new SimpleDateFormat(
						"yyyy-MM-dd HH:mm:ss");
				Date d0 = new java.util.Date();
				Date d1 = df.parse(nextUpdateTime);
				long time0 = d0.getTime();
				long time1 = d1.getTime();
				long mistiming = (time0 - time1) / (1000 * 60 * 60);

				Logger.verifyEquals(true, mistiming <= 24,
						"检查接口返回数据是否为24小时之内更新的数据，此时的时间差为：" + mistiming + "小时");
			}
		  
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_HotList_003() {
		Logger.start(true,"24H买手榜--最有逼格-检查接口返回数据，买手不会重复 ");
		try {
			//设置参数
			String requestid="111111";
			String type = "bigger";
			Date date=new Date();
			DateFormat format= new SimpleDateFormat("yyyyMMddHHmmss" );
			String timenow=format.format( date);

			
			hotlistBean.setInfo("userid:11,cookieid:22,idfa:33,imei:44,yid:3333,timenow:"+timenow+",type:"+type+"");
			hotlistBean.setAppkey("1");
			hotlistBean.setRequestid(requestid);
			hotlistBean.setModule("seller_hot_list");
			hotlistBean.setPlatform("appbuyer");
			hotlistBean.setLimit("0,10");
			
			hotlistCall.setData(hotlistBean);
			hotlistCall.callService();
			
			Logger.verifyEquals("OK", hotlistCall.getStatus(), "验证接口返回状态是否正常");
			
			JSONArray hitsAry = hotlistCall.getHits();
			
			List<String> list = new ArrayList<String>();

			for (int i = 1; i <= hitsAry.length(); i++) {
				
				String ids = hitsAry.get(i - 1).toString();
				JSONObject json = new JSONObject(ids);
				
				String sellerId = json.getString("sellerId");
				
				if (list.contains(sellerId) == true) {

					Logger.verifyEquals(false, list.contains(sellerId), "验证第" + i+ "条返回数据的id存在重复值" + sellerId);

				}else{
					Logger.verifyEquals(false, list.contains(sellerId), "验证第" + i+ "条返回数据的id不存在重复值" + sellerId);
					list.add(sellerId);
				}
			
			}
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_HotList_004() {
		Logger.start(true,"24H买手榜--最有逼格-检查接口每条数据的返回值是否齐全，并且有有效值");
		try {
			//设置参数
			String requestid="111111";
			String type = "bigger";
			Date date=new Date();
			DateFormat format= new SimpleDateFormat("yyyyMMddHHmmss" );
			String timenow=format.format( date);

			
			hotlistBean.setInfo("userid:11,cookieid:22,idfa:33,imei:44,yid:3333,timenow:"+timenow+",type:"+type+"");
			hotlistBean.setAppkey("1");
			hotlistBean.setRequestid(requestid);
			hotlistBean.setModule("seller_hot_list");
			hotlistBean.setPlatform("appbuyer");
			hotlistBean.setLimit("0,10");
			
			hotlistCall.setData(hotlistBean);
			hotlistCall.callService();
			
			Logger.verifyEquals("OK", hotlistCall.getStatus(), "验证接口返回状态是否正常");
			
			JSONArray hitsAry = hotlistCall.getHits();
			
			for(int i=1;i<=hitsAry.length();i++){
				
			 String info = hitsAry.get(i-1).toString();
			 JSONObject infoAry = new JSONObject(info);
			 
			 String sellerId = null;
			 //String logoUrl = null;
			 String fansNum = null;
			 String sellerName = null;
			 String country = null;
			
			 
			 if(infoAry.get("sellerId")!="0" && infoAry.get("sellerId") != null){
				 sellerId = infoAry.getString("sellerId");
			 }
			
			 
			 //获取接口返回的key校验返回字段
			  Logger.verifyEquals(true,infoAry.has("sellerId") , "验证第"+i+"条记录返回结果中有sellerId这个key");
			
			  Logger.verifyNotNull(sellerId, "验证第"+i+"条记录返回结果中有sellerId不为空且不为0");
			 
			}
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_HotList_005() {
		Logger.start(true,"24H买手榜-最受好评-检查接口返回公共信息 ");
		try {
			//设置参数
			String requestid="111111";
			String type = "praise";
			Date date=new Date();
			DateFormat format= new SimpleDateFormat("yyyyMMddHHmmss" );
			String timenow=format.format( date);
			
			hotlistBean.setInfo("userid:11,cookieid:22,idfa:33,imei:44,yid:3333,timenow:"+timenow+",type:"+type+"");
			hotlistBean.setAppkey("1");
			hotlistBean.setRequestid(requestid);
			hotlistBean.setModule("seller_hot_list");
			hotlistBean.setPlatform("appbuyer");
			hotlistBean.setLimit("0,10");
			
			hotlistCall.setData(hotlistBean);
			hotlistCall.callService();
			
			Logger.verifyEquals("OK",hotlistCall.getCommonInfo("status") , "检查接口状态值");
			Logger.verifyNotNull(hotlistCall.getCommonInfo("took"), "took不为空");
			Logger.verifyEquals(false, hotlistCall.getCommonInfoBoo("downgrade"), "验证downgrade返回false");
			Logger.verifyEquals(false, hotlistCall.getCommonInfoBoo("timeout"), "验证timeout返回flase");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_HotList_006() {
		Logger.start(true,"24H买手榜-最受好评-检查接口返回上一次更新时间是否24小时之内 ");
		try {
			//设置参数
			String requestid="111111";
			String type = "praise";
			Date date=new Date();
			DateFormat format= new SimpleDateFormat("yyyyMMddHHmmss" );
			String timenow=format.format( date);
			
			hotlistBean.setInfo("userid:11,cookieid:22,idfa:33,imei:44,yid:3333,timenow:"+timenow+",type:"+type+"");
			hotlistBean.setAppkey("1");
			hotlistBean.setRequestid(requestid);
			hotlistBean.setModule("seller_hot_list");
			hotlistBean.setPlatform("appbuyer");
			hotlistBean.setLimit("0,10");
			
			hotlistCall.setData(hotlistBean);
			hotlistCall.callService();
			
			if (hotlistCall.getStatus() != "OK") {
				Logger.verifyEquals("OK", hotlistCall.getStatus(), "接口返回状态是否异常");
			} else {

				JSONArray hits = hotlistCall.getHits();
				JSONObject searchInfo = (JSONObject) hotlistCall.getSearchInfo();

				String nextUpdateTime = searchInfo.getString("nextUpdateTime");

				SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				Date d0 = new java.util.Date();
				Date d1 = df.parse(nextUpdateTime);
				long time0 = d0.getTime();
				long time1 = d1.getTime();
				long mistiming = (time0 - time1) / (1000 * 60 * 60);

				Logger.verifyEquals(true, mistiming <= 24,"检查接口返回数据是否为24小时之内更新的数据，此时的时间差为：" + mistiming + "小时");
			}
		  
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_HotList_007() {
		Logger.start(true,"24H买手榜--最受好评-检查接口返回数据，买手不会重复 ");
		try {
			//设置参数
			String requestid="111111";
			String type = "praise";
			Date date=new Date();
			DateFormat format= new SimpleDateFormat("yyyyMMddHHmmss" );
			String timenow=format.format( date);
			
			hotlistBean.setInfo("userid:11,cookieid:22,idfa:33,imei:44,yid:3333,timenow:"+timenow+",type:"+type+"");
			hotlistBean.setAppkey("1");
			hotlistBean.setRequestid(requestid);
			hotlistBean.setModule("seller_hot_list");
			hotlistBean.setPlatform("appbuyer");
			hotlistBean.setLimit("0,10");
			
			hotlistCall.setData(hotlistBean);
			hotlistCall.callService();
			
			Logger.verifyEquals("OK", hotlistCall.getStatus(), "验证接口返回状态是否正常");
			
			JSONArray hitsAry = hotlistCall.getHits();
			
			List<String> list = new ArrayList<String>();

			for (int i = 1; i <= hitsAry.length(); i++) {
				
				String ids = hitsAry.get(i - 1).toString();
				JSONObject json = new JSONObject(ids);
				
				String sellerId = json.getString("sellerId");
				
				if (list.contains(sellerId) == true) {

					Logger.verifyEquals(false, list.contains(sellerId), "验证第" + i+ "条返回数据的id存在重复值" + sellerId);

				}else{
					Logger.verifyEquals(false, list.contains(sellerId), "验证第" + i+ "条返回数据的id不存在重复值" + sellerId);
					list.add(sellerId);
				}
			
			}
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_HotList_008() {
		Logger.start(true,"24H买手榜--最受好评-检查接口每条数据的返回值是否齐全，并且有有效值");
		try {
			//设置参数
			String requestid="111111";
			String type = "praise";
			Date date=new Date();
			DateFormat format= new SimpleDateFormat("yyyyMMddHHmmss" );
			String timenow=format.format( date);
			
			hotlistBean.setInfo("userid:11,cookieid:22,idfa:33,imei:44,yid:3333,timenow:"+timenow+",type:"+type+"");
			hotlistBean.setAppkey("1");
			hotlistBean.setRequestid(requestid);
			hotlistBean.setModule("seller_hot_list");
			hotlistBean.setPlatform("appbuyer");
			hotlistBean.setLimit("0,10");
			
			hotlistCall.setData(hotlistBean);
			hotlistCall.callService();
			
			Logger.verifyEquals("OK", hotlistCall.getStatus(), "验证接口返回状态是否正常");
			
			JSONArray hitsAry = hotlistCall.getHits();
			
			for(int i=1;i<=hitsAry.length();i++){
				
			 String info = hitsAry.get(i-1).toString();
			 JSONObject infoAry = new JSONObject(info);
			 
			 String sellerId = null;
			
			 
			 if(infoAry.get("sellerId")!="0" && infoAry.get("sellerId") != null){
				 sellerId = infoAry.getString("sellerId");
			 }
			
			 
			 //获取接口返回的key校验返回字段
			  Logger.verifyEquals(true,infoAry.has("sellerId") , "验证第"+i+"条记录返回结果中有sellerId这个key");
			 
			  Logger.verifyNotNull(sellerId, "验证第"+i+"条记录返回结果中有sellerId不为空且不为0");
			 
			}
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_HotList_009() {
		Logger.start(true,"24H买手榜-黑马榜-检查接口返回公共信息 ");
		try {
			//设置参数
			String requestid="111111";
			String type = "blackhourse";
			Date date=new Date();
			DateFormat format= new SimpleDateFormat("yyyyMMddHHmmss" );
			String timenow=format.format( date);
			
			hotlistBean.setInfo("userid:11,cookieid:22,idfa:33,imei:44,yid:3333,timenow:"+timenow+",type:"+type+"");
			hotlistBean.setAppkey("1");
			hotlistBean.setRequestid(requestid);
			hotlistBean.setModule("seller_hot_list");
			hotlistBean.setPlatform("appbuyer");
			hotlistBean.setLimit("0,10");
			
			hotlistCall.setData(hotlistBean);
			hotlistCall.callService();
			
			Logger.verifyEquals("OK",hotlistCall.getCommonInfo("status") , "检查接口状态值");
			Logger.verifyNotNull(hotlistCall.getCommonInfo("took"), "took不为空");
			Logger.verifyEquals(false, hotlistCall.getCommonInfoBoo("downgrade"), "验证downgrade返回false");
			Logger.verifyEquals(false, hotlistCall.getCommonInfoBoo("timeout"), "验证timeout返回flase");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_HotList_010() {
		Logger.start(true,"24H买手榜-黑马榜-检查接口返回上一次更新时间是否24小时之内 ");
		try {
			//设置参数
			String requestid="111111";
			String type = "blackhourse";
			Date date=new Date();
			DateFormat format= new SimpleDateFormat("yyyyMMddHHmmss" );
			String timenow=format.format( date);
			
			hotlistBean.setInfo("userid:11,cookieid:22,idfa:33,imei:44,yid:3333,timenow:"+timenow+",type:"+type+"");
			hotlistBean.setAppkey("1");
			hotlistBean.setRequestid(requestid);
			hotlistBean.setModule("seller_hot_list");
			hotlistBean.setPlatform("appbuyer");
			hotlistBean.setLimit("0,10");
			
			hotlistCall.setData(hotlistBean);
			hotlistCall.callService();
			
			if (hotlistCall.getStatus() != "OK") {
				Logger.verifyEquals("OK", hotlistCall.getStatus(), "接口返回状态是否异常");
			} else {

				JSONArray hits = hotlistCall.getHits();
				JSONObject searchInfo = (JSONObject) hotlistCall.getSearchInfo();

				String nextUpdateTime = searchInfo.getString("nextUpdateTime");

				SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				Date d0 = new java.util.Date();
				Date d1 = df.parse(nextUpdateTime);
				long time0 = d0.getTime();
				long time1 = d1.getTime();
				long mistiming = (time0 - time1) / (1000 * 60 * 60);

				Logger.verifyEquals(true, mistiming <= 24,"检查接口返回数据是否为24小时之内更新的数据，此时的时间差为：" + mistiming + "小时");
			}
		  
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_HotList_011() {
		Logger.start(true,"24H买手榜--黑马榜-检查接口返回数据，买手不会重复 ");
		try {
			//设置参数
			String requestid="111111";
			String type = "blackhourse";
			Date date=new Date();
			DateFormat format= new SimpleDateFormat("yyyyMMddHHmmss" );
			String timenow=format.format( date);
			
			hotlistBean.setInfo("userid:11,cookieid:22,idfa:33,imei:44,yid:3333,timenow:"+timenow+",type:"+type+"");
			hotlistBean.setAppkey("1");
			hotlistBean.setRequestid(requestid);
			hotlistBean.setModule("seller_hot_list");
			hotlistBean.setPlatform("appbuyer");
			hotlistBean.setLimit("0,10");
			
			hotlistCall.setData(hotlistBean);
			hotlistCall.callService();
			
			Logger.verifyEquals("OK", hotlistCall.getStatus(), "验证接口返回状态是否正常");
			
			JSONArray hitsAry = hotlistCall.getHits();
			
			List<String> list = new ArrayList<String>();

			for (int i = 1; i <= hitsAry.length(); i++) {
				
				String ids = hitsAry.get(i - 1).toString();
				JSONObject json = new JSONObject(ids);
				
				String sellerId = json.getString("sellerId");
				
				if (list.contains(sellerId) == true) {

					Logger.verifyEquals(false, list.contains(sellerId), "验证第" + i+ "条返回数据的id存在重复值" + sellerId);

				}else{
					Logger.verifyEquals(false, list.contains(sellerId), "验证第" + i+ "条返回数据的id不存在重复值" + sellerId);
					list.add(sellerId);
				}
			
			}
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_HotList_012() {
		Logger.start(true,"24H买手榜--黑马榜-检查接口每条数据的返回值是否齐全，并且有有效值");
		try {
			//设置参数
			String requestid="111111";
			String type = "blackhourse";
			Date date=new Date();
			DateFormat format= new SimpleDateFormat("yyyyMMddHHmmss" );
			String timenow=format.format( date);
			
			hotlistBean.setInfo("userid:11,cookieid:22,idfa:33,imei:44,yid:3333,timenow:"+timenow+",type:"+type+"");
			hotlistBean.setAppkey("1");
			hotlistBean.setRequestid(requestid);
			hotlistBean.setModule("seller_hot_list");
			hotlistBean.setPlatform("appbuyer");
			hotlistBean.setLimit("0,10");
			
			hotlistCall.setData(hotlistBean);
			hotlistCall.callService();
			
			Logger.verifyEquals("OK", hotlistCall.getStatus(), "验证接口返回状态是否正常");
			
			JSONArray hitsAry = hotlistCall.getHits();
			
			for(int i=1;i<=hitsAry.length();i++){
				
			 String info = hitsAry.get(i-1).toString();
			 JSONObject infoAry = new JSONObject(info);
			 
			 String sellerId = null;
			 String logoUrl = null;
			 String fansNum = null;
			 String sellerName = null;
			 String country = null;
			
			 
			 if(infoAry.get("sellerId")!="0" && infoAry.get("sellerId") != null){
				 sellerId = infoAry.getString("sellerId");
			 }
			
			 
			 //获取接口返回的key校验返回字段
			  Logger.verifyEquals(true,infoAry.has("sellerId") , "验证第"+i+"条记录返回结果中有sellerId这个key");
			 
			  Logger.verifyNotNull(sellerId, "验证第"+i+"条记录返回结果中有sellerId不为空且不为0");
			
			
			}
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_HotList_013() {
		Logger.start(false,"24H买手榜-传入无效的类型查看接口返回");
		try {
			//设置参数
			String requestid="111111";
			String type = "aaaaa";
			Date date=new Date();
			DateFormat format= new SimpleDateFormat("yyyyMMddHHmmss" );
			String timenow=format.format( date);
			
			hotlistBean.setInfo("userid:11,cookieid:22,idfa:33,imei:44,yid:3333,timenow:"+timenow+",type:"+type+"");
			hotlistBean.setAppkey("1");
			hotlistBean.setRequestid(requestid);
			hotlistBean.setModule("seller_hot_list");
			hotlistBean.setPlatform("appbuyer");
			hotlistBean.setLimit("0,10");
			
			hotlistCall.setData(hotlistBean);
			hotlistCall.callService();
			
			Logger.verifyEquals("NO_RESPONSE",hotlistCall.getCommonInfo("status") , "检查接口状态值");
			Logger.verifyNotNull(hotlistCall.getCommonInfo("took"), "took不为空");
			Logger.verifyEquals(false, hotlistCall.getCommonInfoBoo("downgrade"), "验证downgrade返回false");
			Logger.verifyEquals(false, hotlistCall.getCommonInfoBoo("timeout"), "验证timeout返回flase");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}
